<?php
/*
* File: language.inc
* Author: Franck Degremont
* Date: 05/05/04
* Last modification : March 10, 2005 10:08:08 PM
*/

// Function: language_check
// Description: This function uses the user's language
function language_check() {
	if(session_is_registered('lang')) {
		switch($_SESSION['lang']) {
			case 'fr': include('lang/fr/fr_main.'.PHP_EXT); break;
			default: include('lang/en/en_main.'.PHP_EXT);
		}
	}
	else include('lang/en/en_main.'.PHP_EXT);
}

// Function: language_choose
// Input: String, the chosen language
// Description: This function selects the right language for the user
function language_choose($language) {
	if(isset($language) && !empty($language)) {
		switch($language) {
        	        case 'fr': $_SESSION['lang']='fr'; break;
        	        default: $_SESSION['lang']='en';
        	}
	}
	elseif(!session_is_registered('lang')) $_SESSION['lang']='en';
}


// function getLangSection
// Input :  regExp, the section to retrieve (ex. ^PREF_), array the lang array
// Description : La fonction retourne un tableau contenant tous les
//'SAMPLE_XXX' => $lang['SAMPLE_XXX'] en ajoutant un prefixe à la clé
//'PREFSAMPLE_XXX' => $lang['SAMPLE_XXX'] si fournis, et en fusionnant
// le résultat avec le tableau $arr si fournis
function getLangSection($section,&$lang,$arr='',$pref=''){

	if(!is_array($lang))
		return '';

	ksort($lang);
	reset($lang);
	$in=false;
	$res=array();

	foreach($lang as $key=>$val){
		if(preg_match("/".$section."/i",$key)){
			$in=true;
			$res[$pref.$key]=$val;
			$res[strtoupper($pref.$key)]=$val;

		}
		elseif($in)
			break;
	}

	if(is_array($arr))
		return array_merge($res,$arr);
	else
		return $res;
}


function getTimeText($lang,$time,$timeTC=''){
	if($timeTC=='')
		$timeTC=time();

	$diff=$timeTC-$time;

	if($diff<60) 	// Mˆme minute
		return $lang['MES_TIME_TEXT']['min'][0];
	if($diff<(60*2))	// Inf 2 mins
		return $lang['MES_TIME_TEXT']['min'][1];
	if($diff<(60*60))	// Inf 1 heure
		return sprintf($lang['MES_TIME_TEXT']['min'][2],floor($diff/(60)));
	if($diff<(60*60*2))	// Inf 2 heures
		return sprintf($lang['MES_TIME_TEXT']['hour'][1],floor($diff/(60*60)));
	if($diff<(60*60*24))	// Sup 2 heures
		return sprintf($lang['MES_TIME_TEXT']['hour'][2],floor($diff/(60*60)));
	if($diff<(60*60*24*2))	// Inf 1 jour
		return sprintf($lang['MES_TIME_TEXT']['day'][1],floor($diff/(60*60*24)));
	if($diff>(60*60*24*2))	// Sup 2 jour
		return sprintf($lang['MES_TIME_TEXT']['day'][2],floor($diff/(60*60*24)));

}
?>
